package everyday;

public class T1163 {
    public String lastSubstring(String s) {
        int n = s.length();
        int left = 0;
        int right = 1;
        while (right < n) {
            int k = 0;
            while (right + k < n && s.charAt(left + k) == s.charAt(right + k)) {
                k++;
            }
            if (right + k < n && s.charAt(left + k) < s.charAt(right + k)) {
                int t = left;
                left = right;
                right = Math.max(t + k + 1, right + 1);
            } else {
                right = right + k + 1;
            }
        }
        return s.substring(left);
    }
}
